package com.tgsoft.kdcloud.service.impl;

import cn.hutool.core.date.DateUtil;

import com.tgsoft.security.cache.TokenStoreCache;
import com.tgsoft.security.properties.SecurityExpireProperties;
import com.tgsoft.security.utils.TokenUtils;

import com.tgsoft.kdcloud.entity.SysUserTokenEntity;
import com.tgsoft.kdcloud.service.SysUserTokenService;
import com.tgsoft.kdcloud.vo.SysUserTokenVO;
import lombok.AllArgsConstructor;

import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * 用户Token
 *
 * @author 阿沐 babamu@126.com
 */
@Service
@AllArgsConstructor
public class SysUserTokenServiceImpl  implements SysUserTokenService {
    private final TokenStoreCache tokenStoreCache;
    private final SecurityExpireProperties securityExpireProperties;

    @Override
    public SysUserTokenVO createToken(Long userId) {
        // 生成token
        String accessToken = TokenUtils.generator();
        String refreshToken = TokenUtils.generator();

        SysUserTokenEntity entity = new SysUserTokenEntity();
        entity.setUserId(userId);
        entity.setAccessToken(accessToken);
        entity.setRefreshToken(refreshToken);

        // 过期时间
        Date now = new Date();
        entity.setAccessTokenExpire(DateUtil.toLocalDateTime(DateUtil.offsetSecond(now, securityExpireProperties.getAccessTokenExpire())));
        entity.setRefreshTokenExpire(DateUtil.toLocalDateTime(DateUtil.offsetSecond(now, securityExpireProperties.getRefreshTokenExpire())));


        return new  SysUserTokenVO(accessToken,refreshToken,entity.getAccessTokenExpire(),entity.getRefreshTokenExpire());
        //return SysUserTokenConvert.INSTANCE.convert(entity);
    }

    @Override
    public SysUserTokenVO refreshToken(String refreshToken) {
        return null;
    }


    @Override
    public void expireToken(Long userId) {
        SysUserTokenEntity entity = new SysUserTokenEntity();
        LocalDateTime now = LocalDateTime.now();
        entity.setAccessTokenExpire(now);
        entity.setRefreshTokenExpire(now);


    }


}